import Member from "@/common/model/Member"
import Step from "@/common/model/Step"

/******************** 定义类 ***************************************************************/
/*
    管理员角色类
 */
function Service() {
    this.title = ""; // 标题
    this.number = "";//编号
    this.id = null;
    this.service_type = ""; //服务中类
    this.member_id = ""; //服务机构id
    this.member = new Member();
    this.service_content = "";  //服务内容
    this.service_people = "";//服务对象
    this.requirement = "";  //申报条件
    this.service_feature = ""; //服务特色
    this.service_process = new Step();//服务流程
    this.service_department = "";//服务部门
    this.service_master = "";//联系人
    this.service_tele = "";//联系电话
    this.service_address = "";//联系地址
    //this.approve_time = "";//审批时间
    this.needFileds = [];//需要文件
    this.Remark = "";//备注
    this.create_time = ""; // 创建时间
    this.finish_time = "";//结束时间
    this.formated_create_time="";//格式化时间
    this.appendix = []; // 附件
    this.appendix_ids=[];
    this.status=null;//状态
}

// 将对象序列化为一个json对象，可以用于http请求或对象复制
Service.prototype.serialize = function() {
    return Object.assign({}, this, {
        needFileds: this.needFileds.join(","),
        appendix_ids: this.appendix.map(function(elem){return elem.id}).join(","),
        service_process: JSON.stringify(this.service_process),
        finish_time : Date.parse(this.finish_time)
    });
}
// 用json数据为对象属性赋值
Service.prototype.parse = function(data) {
    Object.assign(this, data, {
        needFileds: data.needFileds ? data.needFileds.split(",") : [],
        create_time: new Date(parseInt(data.create_time)).toLocaleDateString(),
        finish_time: new Date(parseInt(data.finish_time)).toLocaleDateString(),
        //+" "+new Date(parseInt(data.create_time)).getHours()+":"+new Date(parseInt(data.create_time)).getMinutes()
        appendix_ids: data.appendix_ids.split(","),
        member: Member.parse(data.member || {}),
        service_process: data.service_process ? JSON.parse(data.service_process) : new Step()
    });
}

// 暂时将属性存起来，做一次备份
Service.prototype.storageValue = function() {
    this.storage = this.serialize();
}

// 用备份的数据覆盖现有数据
Service.prototype.applyStorageValue = function() {
    if(this.storage) {
        this.parse(this.storage);
    }
}

// ******************** 类静态属性与方法 **********************************************
// 解析json数据，返回一个对象
Service.parse = function(data) {
    var o = new Service();
    o.parse(data);
    return o;
}

// 解析json数组，返回对象数组
Service.parseList = function(list) {
    var len = list.length;
    var arr = new Array(len);
    for(var i = 0; i < len; i++) {
        arr[i] = Service.parse(list[i]);
    }
    return arr;
}

// ******************** 数据库存储方法 **********************************************
// Investment.prototype.save = function(callback) {
//     var that = this;
//     var action = this.mode == "add" ? "article_add" : "article_modify";
//     api.post(action, this.serialize(), function(res) {
//         that.id = res.id;
//         if(callback) callback();
//     });
// }



// ******************** 导出 **********************************************
export default Service